package com.chaopei.day36.tools;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.chaopei.day36.entity.Cool;

import javax.persistence.Index;
import java.util.List;
import java.util.Map;

/**
 * @Author Corday 天涯
 * @Date 2022/4/20 14:21
 * json字符串与java对象的解析出
 */
public class MyTools {
    //1.java对象转标准json字符串
    public static String javaToJson(Object obj) {
        //普通的java自定义对象
        Cool cool = new Cool(101, "tom");
        //解析为json字符串
        String json = JSONObject.toJSONString(cool);
        JSON.toJSONString(obj);
        return JSONObject.toJSONString(obj);
    }

    //2.将json字符串解析并转换为java对象
    //2.1转成Cool
    public static void jsonToJava1(String json) {
        String str = "{'add':'郑州市五四大街','arr':[{'id':8,'name':'tom'},{'id':9,'name':'bean'}]," +
                "'cool':{'id':10,'name':'mary'}}";
        //(1)将json字符串先解析出来
        JSONObject o = JSONObject.parseObject(str);
        Object obj = o.get("add");
        System.out.println("obj:" + obj);
        String temp = o.getString("add");
        System.out.println("temp:" + temp);
        System.out.println("----解析出Cool对象---");
        JSONObject oo = o.getJSONObject("cool");
        Cool cool = oo.toJavaObject(Cool.class);
        System.out.println(cool);
    }

    //2.2转成List<Cool>
    public static void jsonToJava2(String json) {
        String str = "{'add':'郑州市五四大街','arr':[{'id':8,'name':'tom'},{'id':9,'name':'bean'}]," +
                "'cool':{'id':10,'name':'mary'}}";
        //获取json对象
        JSONObject obj = JSONObject.parseObject(str);
        //获取json数组对象
        JSONArray arr = obj.getJSONArray("arr");
        List<Cool> list = arr.toJavaList(Cool.class);
        System.out.println(list);
        System.out.println(list.get(0).getName());
        System.out.println("--------------");
        Object[] objs = arr.toArray();
        System.out.println(objs);
        System.out.println(objs[0]);
    }

    //2.3转成Map<Integer,Cool>
    public static void jsonToJava3(String json) {
        String str = "{'1':{'id':20,'name':'tina'},'2':{'id':21,'name':'mick'}}";
        JSON j = JSONObject.parseObject(str);
        ////(1)转成map
        Map map = JSONObject.toJavaObject(j, Map.class);
        System.out.println(map.get("1"));
        //拿出对象的字符串
        String s = map.get("2").toString();
        //再转成json对象
        JSONObject temp = JSON.parseObject(s);
        //最后转为java对象
        Cool cool = temp.toJavaObject(Cool.class);
        System.out.println("拿cool:" + cool.getName());
        System.out.println("--------------------");
        ////(2)再转map
        String ss = "{'muti':{1:{'id':10,'name':'李娜'},2:{'id':11,'name':'胡月'}}}";
        JSONObject object = JSONObject.parseObject(ss);
        JSONObject oo = object.getJSONObject("muti");
        //转成map对象
        Map maps = oo.toJavaObject(Map.class);
        String hh = maps.get("2").toString();
        JSONObject mm = JSONObject.parseObject(hh);
        Cool co = mm.toJavaObject(Cool.class);
        System.out.println("转cool了:"+co.getName());

    }

}
